package theOrbalEra.common;

import java.util.Random;

import theOrbalEra.common.item.*;
import theOrbalEra.common.network.PacketPlayerHandler;
import theOrbalEra.common.player.PlayerHandler;
import theOrbalEra.common.util.idList.PlayerActionList;

import net.minecraft.client.audio.SoundManager;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.boss.EntityDragon;
import net.minecraft.entity.boss.EntityWither;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityBlaze;
import net.minecraft.entity.monster.EntityCaveSpider;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntityEnderman;
import net.minecraft.entity.monster.EntityGhast;
import net.minecraft.entity.monster.EntityGolem;
import net.minecraft.entity.monster.EntityMagmaCube;
import net.minecraft.entity.monster.EntityPigZombie;
import net.minecraft.entity.monster.EntitySilverfish;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntitySlime;
import net.minecraft.entity.monster.EntitySnowman;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.monster.EntityWitch;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.passive.EntityBat;
import net.minecraft.entity.passive.EntityChicken;
import net.minecraft.entity.passive.EntityCow;
import net.minecraft.entity.passive.EntityMooshroom;
import net.minecraft.entity.passive.EntityOcelot;
import net.minecraft.entity.passive.EntityPig;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.passive.EntitySquid;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.client.event.sound.SoundLoadEvent;
import net.minecraftforge.event.Event.Result;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.IPlayerTracker;
import cpw.mods.fml.relauncher.Side;

public class OrbalEventHandler implements IPlayerTracker{
	
	@ForgeSubscribe
	public void getXPAndplayerDead(LivingDeathEvent event) {
		EntityLiving living=event.entityLiving;
		
		String deadWay=event.source.getDamageType();
		if(deadWay.equals("player")){
			String username=event.source.getSourceOfDamage().getEntityName();
		playerUpgrade(username, living);
		dropElment(living, username);
		}
		if(living instanceof EntityPlayerMP){
			if(FMLCommonHandler.instance().getEffectiveSide()==Side.SERVER){
				PacketPlayerHandler.playerAction(PlayerActionList.DEAD, 0, ((EntityPlayerMP)living).username);
			
			}

		}
	}
	@ForgeSubscribe
	public void playerHurt(LivingHurtEvent event) {
		EntityLiving living=event.entityLiving;
		if(living instanceof EntityPlayerMP){
			int playerHP=PlayerHandler.getPlayer(((EntityPlayerMP)living).username).getStats().HP;
			if(playerHP-20-event.ammount>=0){
				event.ammount=0;
				}
			else{
				PacketPlayerHandler.playerAction(PlayerActionList.HURT,event.ammount,((EntityPlayerMP)living).username);
			}
			
		}
	}
	
	@ForgeSubscribe
	  public void fillBag(EntityItemPickupEvent event){
		
		ItemStack itemStack=event.item.getEntityItem();
		if(itemStack.getItem() instanceof OrbalItemElement){
			EntityPlayer player = event.entityPlayer;
			if(OrbalItemEleBag.hasbag(player)){
				int bagPos;
				if((bagPos=OrbalItemEleBag.getBaginInv(player))!=-1){
					int[] ele=new int[7];
				 ele=OrbalItemEleBag.getEleCount(player.inventory.mainInventory[bagPos]);
				ele[((OrbalItemElement)itemStack.getItem()).ele]+=itemStack.stackSize;
				OrbalItemEleBag.setEleCount(player.inventory.mainInventory[bagPos], ele);
				event.item.getEntityItem().stackSize=0;
				}
			}
		}
	}
//	@ForgeSubscribe
//	  public void loadSounds(SoundLoadEvent event){
//		SoundManager soundmanger=event.manager;
//		//TODO add cutoms sounds
//	}
	
	private void playerUpgrade(String username, EntityLiving living) {
		int experience=0;
		if(living instanceof EntityBat){
			experience=5;
			
		}
		if(living instanceof EntityChicken){
			experience=2;
			
		}
		if(living instanceof EntityCow){
			experience=2;
			
		}
		if(living instanceof EntityMooshroom){
			experience=10;
			
		}
		if(living instanceof EntityOcelot){
			experience=5;
			
		}
		if(living instanceof EntityPig){
			experience=2;
			
		}
		if(living instanceof EntitySheep){
			experience=2;
			
		}
		if(living instanceof EntitySquid){
			experience=2;
			
		}
		if(living instanceof EntityVillager){
			experience=10;
			
		}
		if(living instanceof EntityEnderman){
			experience=20;
			
		}
		if(living instanceof EntityWolf){
			experience=15;
			
		}
		if(living instanceof EntityPigZombie){
			experience=10;
			
		}
		if(living instanceof EntityBlaze){
			experience=15;
			
		}
		if(living instanceof EntityCaveSpider){
			experience=5;
			
		}
		if(living instanceof EntityCreeper){
			experience=10;
			
		}
		if(living instanceof EntityGhast){
			experience=15;
			
		}
		if(living instanceof EntityMagmaCube){
			experience=2;
			
		}
		if(living instanceof EntitySilverfish){
			experience=3;
			
		}
		if(living instanceof EntitySkeleton){
			experience=5;
			
		}
		if(living instanceof EntitySlime){
			experience=1;
			
		}
		if(living instanceof EntitySpider){
			experience=5;
			
		}
		if(living instanceof EntityWitch){
			experience=20;
			
		}
		if(living instanceof EntityZombie){
			experience=5;
			
		}
		if(living instanceof EntityGolem){
			experience=5;
			
		}
		if(living instanceof EntitySnowman){
			experience=2;
			
		}
		if(living instanceof EntityDragon){
			experience=100;
			
		}
		if(living instanceof EntityWither){
			experience=100;
			
		}
	    PacketPlayerHandler.playerAction(PlayerActionList.EXPERIENCE_UP, experience, username);	
	}




	private void dropElment(EntityLiving living,String username) {
		Random ran= new Random();
		
		if(FMLCommonHandler.instance().getEffectiveSide()==Side.SERVER){
			World world=FMLCommonHandler.instance().getMinecraftServerInstance().worldServerForDimension(FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().getPlayerForUsername(username).dimension);
			int[] drop={0,0,0,0,0,0,0};
		if(living instanceof EntityBat){
			drop[0]=0;
			drop[1]=0;
			drop[2]=0;
			drop[3]=2+ran.nextInt(2);
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityChicken){
			drop[0]=0;
			drop[1]=0;
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityCow){
			drop[0]=0;
			drop[1]=0;
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityPig){
			drop[0]=0;
			drop[1]=0;
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityMooshroom){
			drop[0]=0;
			drop[1]=0;
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityOcelot){
			drop[0]=0;
			drop[1]=0;
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntitySheep){
			drop[0]=0;
			drop[1]=0;
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntitySquid){
			drop[0]=0;
			drop[1]=10+ran.nextInt(5);
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityVillager){
			drop[0]=0+ran.nextInt(3);
			drop[1]=0;
			drop[2]=0;
			drop[3]=0;
			drop[4]=2+ran.nextInt(4);
			drop[5]=2+ran.nextInt(4);
			drop[6]=0;
			
		}
		if(living instanceof EntityEnderman){
			drop[0]=0;
			drop[1]=0;
			drop[2]=0;
			drop[3]=0;
			drop[4]=10+ran.nextInt(10);
			drop[5]=10+ran.nextInt(10);
			drop[6]=10+ran.nextInt(10);
			
		}
		if(living instanceof EntityWolf){
			drop[0]=10+ran.nextInt(6);
			drop[1]=0;
			drop[2]=2+ran.nextInt(3);
			drop[3]=5;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityPigZombie){
			drop[0]=20+ran.nextInt(10);
			drop[1]=0;
			drop[2]=10;
			drop[3]=0;
			drop[4]=0;
			drop[5]=4;
			drop[6]=0;
			
		}
		if(living instanceof EntityBlaze){
			drop[0]=10+ran.nextInt(10);
			drop[1]=0;
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=4;
			drop[6]=3;
			
		}
		if(living instanceof EntityCaveSpider){
			drop[0]=0;
			drop[1]=0;
			drop[2]=10;
			drop[3]=10;
			drop[4]=0;
			drop[5]=0;
			drop[6]=2;
			
		}
		if(living instanceof EntityCreeper){
			drop[0]=20;
			drop[1]=0;
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityGhast){
			drop[0]=10;
			drop[1]=0;
			drop[2]=0;
			drop[3]=10;
			drop[4]=0;
			drop[5]=0;
			drop[6]=5;
			
		}
		if(living instanceof EntityMagmaCube){
			drop[0]=10;
			drop[1]=0;
			drop[2]=2;
			drop[3]=0;
			drop[4]=0;
			drop[5]=3;
			drop[6]=0;
			
		}
		if(living instanceof EntitySilverfish){
			drop[0]=0;
			drop[1]=0;
			drop[2]=10;
			drop[3]=20;
			drop[4]=10;
			drop[5]=0;
			drop[6]=1;
			
		}
		if(living instanceof EntitySkeleton){
			drop[0]=0;
			drop[1]=0;
			drop[2]=0;
			drop[3]=10;
			drop[4]=0;
			drop[5]=5;
			drop[6]=0;
			
		}
		if(living instanceof EntitySlime){
			drop[0]=0;
			drop[1]=10;
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=0;
			drop[6]=4;
			
		}
		if(living instanceof EntitySpider){
			drop[0]=0;
			drop[1]=0;
			drop[2]=3;
			drop[3]=2;
			drop[4]=0;
			drop[5]=1;
			drop[6]=0;
			
		}
		if(living instanceof EntityWitch){
			drop[0]=20;
			drop[1]=20;
			drop[2]=20;
			drop[3]=20;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityZombie){
			drop[0]=0;
			drop[1]=0;
			drop[2]=10;
			drop[3]=5;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityGolem){
			drop[0]=0;
			drop[1]=0;
			drop[2]=30;
			drop[3]=5;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0+ran.nextInt(20);
			
		}
		if(living instanceof EntitySnowman){
			drop[0]=0;
			drop[1]=5+ran.nextInt(5);
			drop[2]=0;
			drop[3]=0;
			drop[4]=0;
			drop[5]=0;
			drop[6]=0;
			
		}
		if(living instanceof EntityDragon){
			drop[0]=64;
			drop[1]=64;
			drop[2]=64;
			drop[3]=64;
			drop[4]=64;
			drop[5]=64;
			drop[6]=64;
			
		}
		if(living instanceof EntityWither){
			drop[0]=64;
			drop[1]=64;
			drop[2]=64;
			drop[3]=64;
			drop[4]=64;
			drop[5]=64;
			drop[6]=64;
			
		
			}

		if(drop[0]!=0){
			ItemStack fire=new ItemStack(TheOrbalEra.fireElement,drop[0]);
			EntityItem item=new EntityItem(world,living.posX+ran.nextDouble(),living.posY+ran.nextDouble(),living.posZ+ran.nextDouble(),fire);
			world.spawnEntityInWorld(item);
			}
			if(drop[1]!=0){
				ItemStack water=new ItemStack(TheOrbalEra.waterElement,drop[1]);
				EntityItem item=new EntityItem(world,living.posX+ran.nextDouble(),living.posY+ran.nextDouble(),living.posZ+ran.nextDouble(),water);
				world.spawnEntityInWorld(item);
				}
			if(drop[2]!=0){
				ItemStack earth=new ItemStack(TheOrbalEra.earthElement,drop[2]);
				EntityItem item=new EntityItem(world,living.posX+ran.nextDouble(),living.posY+ran.nextDouble(),living.posZ+ran.nextDouble(),earth);
				world.spawnEntityInWorld(item);
				}
			if(drop[3]!=0){
				ItemStack earth=new ItemStack(TheOrbalEra.windElement,drop[3]);
				EntityItem item=new EntityItem(world,living.posX+ran.nextDouble(),living.posY+ran.nextDouble(),living.posZ+ran.nextDouble(),earth);
				world.spawnEntityInWorld(item);
				}
			if(drop[4]!=0){
				ItemStack earth=new ItemStack(TheOrbalEra.timeElement,drop[4]);
				EntityItem item=new EntityItem(world,living.posX+ran.nextDouble(),living.posY+ran.nextDouble(),living.posZ+ran.nextDouble(),earth);
				world.spawnEntityInWorld(item);
				}
			if(drop[5]!=0){
				ItemStack earth=new ItemStack(TheOrbalEra.spaceElement,drop[5]);
				EntityItem item=new EntityItem(world,living.posX+ran.nextDouble(),living.posY+ran.nextDouble(),living.posZ+ran.nextDouble(),earth);
				world.spawnEntityInWorld(item);
				}
			if(drop[6]!=0){
				ItemStack earth=new ItemStack(TheOrbalEra.unrealElement,drop[6]);
				EntityItem item=new EntityItem(world,living.posX+ran.nextDouble(),living.posY+ran.nextDouble(),living.posZ+ran.nextDouble(),earth);
				world.spawnEntityInWorld(item);
				}
		}
	}
	@Override
	public void onPlayerLogin(EntityPlayer player) {
		
		
	}
	@Override
	public void onPlayerLogout(EntityPlayer player) {
		
		
	}
	@Override
	public void onPlayerChangedDimension(EntityPlayer player) {
		
		
	}
	@Override
	public void onPlayerRespawn(EntityPlayer player) {
		PacketPlayerHandler.playerAction(PlayerActionList.REBORN, 0,player.username);
		
	}	
	










}
  